package com.aisino.wxkp.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.aisino.wxkp.common.basic.WxkpBaseAction;
import com.aisino.wxkp.common.config.Constants;
import com.aisino.wxkp.common.entity.CADetails;
import com.aisino.wxkp.common.entity.CAInfo;
import com.aisino.wxkp.common.entity.CAInfosReturn;
import com.aisino.wxkp.common.entity.JsAeroInfoReturnException;
import com.aisino.wxkp.common.util.AjaxUtil;
import com.aisino.wxkp.common.util.Base64G;
import com.aisino.wxkp.common.util.HttpReqHelper;
import com.aisino.wxkp.common.util.HxJSONUtils;
import com.aisino.wxkp.common.util.StringUtil;

public class WXBussinessAction extends WxkpBaseAction{
	
	
	/**
	  * 模糊查询企业信息
	  * @param fqymc
	  * @return
	  */
	 public static void CAInfosByFQ(HttpServletRequest request,HttpServletResponse response){
		 try {
			response.setContentType("application/json;charset=utf-8 ");
			String fqymc = request.getParameter("fqymc");
			CAInfosReturn caInfos = null;
			List<CAInfo> list = new ArrayList<CAInfo>();
			if(StringUtil.validateNull(fqymc)){
				throw new JsAeroInfoReturnException("企业名称为null");
			}
			String retCAInfos = HttpReqHelper.sendPost(Constants.CAInfosByFQ+"?symbol=prefixQuery&InterfaceKind=prefixQuery&q="+fqymc+"", "");
			System.out.println(retCAInfos);
			caInfos = (CAInfosReturn) HxJSONUtils.getDTO(retCAInfos, CAInfosReturn.class);
			if("S0000".equals(caInfos.getCode())){
				System.out.println("接口返回企业："+retCAInfos);
				JSONObject obj = new JSONObject(retCAInfos);
				JSONArray jsonArray = obj.getJSONArray("result"); 
				for (int i=0; i<jsonArray.length(); i++) {
					JSONObject object = (JSONObject) jsonArray.get(i);
					String code = object.getString("code");
					String name = object.getString("name");
					CAInfo caInfo  = new CAInfo(code, name);
					list.add(i, caInfo);
				}
		        String sjson = HxJSONUtils.getJSONString(list);
				writeToResponseSize(response,sjson);
			}else {
				throw new JsAeroInfoReturnException("连接超时");
			}
				
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
	 /**
	  * 根据六位码获取企业信息
	  * @param codeNo
	  * @return
	  */
	 public static  void CAInfoByCode(HttpServletRequest request,HttpServletResponse response){
		 try {
			response.setContentType("application/json;charset=utf-8 ");
			String codeNo = request.getParameter("codeNo").trim();
			System.out.println("codeNo=="+codeNo);
			if(StringUtil.validateNull(cod    eNo)){
				throw new JsAeroInfoReturnException("六位码为空");
			}
			String CAResult= HttpReqHelper.sendPost(Constants.CAInfosByFQ+"?symbol=billingByCode&InterfaceKind=billingByCode&code="+codeNo+"", "");
			System.out.println("接口返回企业明细："+CAResult);
			writeToResponseSize(response,CAResult);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	}

}
